<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon
              icon-class="home"
            />
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">

                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-card>

    </div>
    <el-dialog v-if="showDialog" :title="tj ?'添加':'编辑'" :visible.sync="showDialog" :show-close="true" :close-on-press-escape="true" :close-on-click-modal="false" @before-close="beforeclose" @close="close">
      <Adddept v-if="tj" :parentid="parentid" :nocodecf="nocodecf" :jisuan="jisuan" @closea="closea" @closeaa="closeaa" />
      <Bianji v-else :bianjiid="bianjiid" :nocodecf="nocodecf" :jisuanbj="jisuanbj" @closee="closee" @abc="abc" @genxin="genxin" />
    </el-dialog>

    <!--这个地方根据配置项全部显示  :default-expand-all="true" -->
    <el-tree
      :data="list"
      :props="defaultProps"
      :default-expand-all="true"
    >   <template #default="{ data }">
      <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
      <!--
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (table 单元格数据渲染)
    -->
      <el-row
        type="flex"
        justify="space-between"
        align="middle"
        style="height: 40px; width: 100%;"
      >
        <el-col :span="20">
          <svg-icon
            icon-class="ryuan"
          />
          <span>{{ data.name }}</span>
        </el-col>
        <el-col :span="4">
          <el-row type="flex" justify="end">
            <!-- 两个内容 -->
            <el-col>{{ }}</el-col>
            <el-col>{{ }}</el-col>
            <el-col class="aaa">{{ }}</el-col>

            <el-col v-if="data.manager&&data.manager!='管理员' " class="aaa">{{ data.manager }}</el-col>
            <el-col>
              <!-- 下拉菜单 element -->
              <el-dropdown>
                <span class="mr"> 操作<i class="el-icon-arrow-down" /> </span>
                <!-- 下拉菜单 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item @click.native="addprot(data.id)">添加子部门</el-dropdown-item>
                  <el-dropdown-item @click.native="heidport(data.id)">编辑部门</el-dropdown-item>
                  <el-dropdown-item @click.native="delport(data.id)">删除部门</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </template></el-tree>
  </div>

</template>
<!-- 放置一个el-tree组件 -->

<script>
import { getDepartments, delDepartment } from '../../api/departments'
import { tranListToTreeData } from '../../utils/tree'
import Adddept from './components/add-dept'
import Bianji from './components/bianji'
export default {
  components: {
    Adddept,
    Bianji
  },
  data() {
    return {
      showDialog: false, // 控制子组件显示与隐藏
      // 依赖一份树形数据
      list: [],
      defaultProps: {
        // 这个地方可以自定义更改后台传过来的列表名字,是个自定义配置项
        label: 'name',
        children: 'children'
      },
      parentid: null,
      bianji: false, // 是否处于编辑状态
      // pid: null
      tj: false,
      bianjiid: null,
      nocodecf: [],
      noname: []

    }
  },
  computed: {
    jisuan() {
      return this.noname.filter(item => item.pid === this.parentid).map(item => item.name)
    },
    jisuanbj() {
      // console.log(this.bianjiid)
      const arr = this.noname.find(item => {
        return item.id === this.bianjiid
      })
      // console.log(arr)
      console.log(this.noname.filter(item => item.pid === arr.pid).map(item => item.name))
      return this.noname.filter(item => item.pid === arr.pid).map(item => item.name)
    }
  },
  created() {
    // 平铺数据
    this.getDepartments()
  },
  methods: {
    closeaa() {
      this.showDialog = false
    },
    beforeclose(done) {
      done()
      console.log(123)
    },
    closea() {
      this.showDialog = false
      // this.bianji = false

      this.tj = false
      this.getDepartments()
    },
    genxin() {
      this.getDepartments()
    },
    closee() {
      this.getDepartments()
      this.tj = false
      this.$message({
        showClose: true,
        message: '更新成功',
        type: 'success'
      })
      this.showDialog = false
    },
    abc() {
      // console.log(1)
      this.showDialog = false
    },
    // 获取平铺数据
    async getDepartments() {
      const res = await getDepartments()
      console.log(res.data)
      this.nocodecf = res.data.depts.map(res => {
        return res.code
      })
      this.noname = res.data.depts.map(res => {
        return {
          id: res.id,
          pid: res.pid,
          name: res.name
        }
      })
      // console.log(this.noname)
      // console.log(this.nocodecf)
      // 获取平铺数据,获取完毕后转树
      this.list = tranListToTreeData(res.data.depts)
      // 调用utils转树
    },
    close() {
      this.tj = false
      this.showDialog = false
      // console.log(1)
    },
    addprot(id) {
      // console.log(this.tj, this.showDialog)
      // 添加部门方法
      this.tj = true
      this.showDialog = true // 点击让子组件显示

      // 记录当前id然后保存在data里方便传给后台
      this.parentid = id

      // 去子组件
    },
    async delport(id) {
      // 删除部门方法.id记录去写接口
      // console.log(id)
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 1. 去做删除
        console.log('用户点击删除', id)
        const rs = await delDepartment(id)
        console.log('删除之后的返回值', rs)
        // 2. 提示用户
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // 3. 更新页面- 重发请求
        this.getDepartments()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    updatedepart() {
      // 子传父通知关闭弹窗
      this.showDialog = false
      // 更新数据
      this.getDepartments()
    },
    heidport(id) {
      this.bj = true
      // console.log(this.parentid)
      this.showDialog = true
      // this.bianji = true
      this.bianjiid = id

      // console.log(this.parentid)
    }

  }

}
</script>
<style scoped >

.aaa{
  margin-right: 30px;
}

</style>
